package com.ruoyi.web.controller.users;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.Student;
import com.ruoyi.system.domain.SysAnnounManagement;
import com.ruoyi.system.domain.SyseEventsPosted;
import com.ruoyi.system.domain.mongodb.Schools;
import com.ruoyi.system.domain.mongodb.Unrels;
import com.ruoyi.system.domain.mongodb.Users;
import com.ruoyi.system.domain.mongodb.Works;
import com.ruoyi.system.mongo.StudentDao;
import com.ruoyi.system.service.IStudentService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/student")
public class StudentController {

    @Autowired
    private IStudentService iStudentService;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Autowired
    private StudentDao studentDao;

    /**
     * 根据id修改手机号信息(手机认证)
     * @param student
     */
    @ApiOperation("根据id修改手机号信息(手机认证)")
    @PutMapping("/updateStudnetPhoneNumById")
    public AjaxResult updateStudnetPhoneNumById(Student student) {
        iStudentService.updateStudnetPhoneNumById(student);
        return AjaxResult.success();
    }


    /**
     * 根据大赛id查询参与该大赛的所有学生信息
     */
    @ApiOperation("根据大赛id查询参与该大赛的所有学生信息")
    @GetMapping("/queryStudentsByEpid/{epid}")
    public AjaxResult queryStudentsByEpid(@PathVariable Integer epid){
       return AjaxResult.success(iStudentService.queryStudentsByEpid(epid));
    }



    /**
     *根据学校名称进行查询  返回该学校下面的所有学生
     * */
    @ApiOperation("根据学校名称进行查询")
    @GetMapping("selectUsersMG")
    public PageInfo<Users> selectUsersMG(@RequestParam("schoolsName") String schoolsName,Integer pageNum,Integer pageSize){
        PageInfo<Users> pageInfo = iStudentService.selectBySchool(schoolsName,pageNum,pageSize);
        return pageInfo;
    }

    /**
     * 管理端确认报名
     * */
    @GetMapping("selectUsersById")
    @ApiOperation("管理端确认报名")
    public Integer selectUsersById(@RequestParam("studentArr") String studentArr,@RequestParam("epid") String epId,String teacherId){
        Integer flag = iStudentService.selectbyUserId(studentArr,epId,teacherId);
        return flag;
    }

    /**
     * 报名管理查询
     * eptitle：赛事标题
     * epsponsor 主办方
     * epstate 赛事状态
     * eplevel 赛事级别
     */
    @GetMapping("selectEventsPostedAndAggregation")
    public PageInfo<SyseEventsPosted> selectEventsPostedAndAggregation(String eptitle,String epsponsor,String epstate,String eplevel,Integer pageNum,Integer pageSize){
        List<SyseEventsPosted> syseEventsPostedList = iStudentService.selectEventsPostedAndAggregation(eptitle,epsponsor,epstate,eplevel,pageNum,pageSize);
        PageInfo pageInfo = new PageInfo(syseEventsPostedList);
        return pageInfo;
    }

    /**
     * 报名学生列表查询
     * eptitle：赛事标题
     * realname 学生姓名
     * epstate 赛事状态
     * eplevel 赛事级别
     */
    @GetMapping("selectEventsPostedForStudents")
    public PageInfo<Student> selectEventsPostedForStudents(Student student,Integer pageNum,Integer pageSize){
        System.out.println(student);
        List<Student> studentList = iStudentService.selectEventsPostedForStudents(student,pageNum,pageSize);
        PageInfo pageInfo = new PageInfo(studentList);
        return pageInfo;
    }


    /**
     * 根据赛事下面的学生
     * epid 赛事的id
     * */
    @GetMapping("selectByEpIdAndEpstate")
    public PageInfo<Student> selectByEpIdAndEpstate(String epid,Integer pageNum,Integer pageSize){
        if(StringUtils.isNotEmpty(epid)){
            List<Student> studentList = iStudentService.selectByEpIdAndEpstate(epid,pageNum,pageSize);
            PageInfo pageInfo = new PageInfo(studentList);
            return pageInfo;
        }
        return new PageInfo();
    }

    /**
     * 报名审批接口
     * @param sid   学生ID
     * @param epId  赛事ID
     * @param status 审批状态：2=通过，3=不通过
     * @return
     */
    @ApiOperation("报名审批接口")
    @GetMapping("baoMingShenPi")
    public Integer baoMingShenPi(String sid, String epId, int status) {
       return iStudentService.baoMingShenPi(sid, epId, status);
    }

    /**
     * 批量报名审批接口
     * @param students   学生对象
     * @return
     */
    @ApiOperation("批量报名审批接口")
    @PostMapping("piliangShenPi")
    public Integer piliangShenPi(@RequestBody List<Student> students) {
        return iStudentService.piliangShenPi(students);
    }

    // 删除已经报名赛事得学生
    @GetMapping("deleteStudentBySid")
    public Integer deleteStudentBySid(String sid,String epId){
        return iStudentService.deleteStudentBySid(sid,epId);
    }


    /**
     * 根据 学校名称查询学校下面得老师
     * */
    @GetMapping("selectUsersByRole")
    public List<Users> selectUsersByRole(@RequestParam("schoolName") String schoolName){
        return iStudentService.selectUsersByRole(schoolName);
    }

    /**
     * 根据学生ID查询该学生所报名的赛事
     * */
    @ApiOperation("根据学生ID查询该学生所报名的赛事")
    @ApiImplicitParam(name = "userId", value = "学生ID", required = true, dataType = "String")
    @GetMapping("selectEventsByUserID")
    public List<Map<String, Object>> selectEventsByUserID(String userId){
        return iStudentService.selectEventsByUserID(userId);
    }

    /**
     * 根据学生ID查询该学生所报名的赛事
     * */
    @ApiOperation("根据老师ID查询该学生所报名的赛事")
    @ApiImplicitParam(name = "teacherId", value = "老师ID", required = true, dataType = "String")
    @GetMapping("selectEventsByTeacherID")
    public List<Map<String, Object>> selectEventsByTeacherID(String teacherId){
        return iStudentService.selectEventsByTeacherID(teacherId);
    }


    /**
     * 根据老师所在得学校 查询这个学校下得所有学生
     *  teacherId:老师id
     * */
    @GetMapping("selectByTeacherSchool")
    public PageInfo<Users> selectByTeacherSchool(String teacherId,Integer pageNum,Integer pageSize){
        PageInfo<Users> usersList = iStudentService.selectByTeacherSchool(teacherId, pageNum, pageSize);
//        PageInfo pageInfo = new PageInfo(usersList);
        return usersList;
    }


    /**
     * 老师端得学生批量报名
     * teacherId:老师id  操作人id
     * epid：赛事id
     * studentArr：学生id
     * groupId: 赛事小组
     * */
    @RequestMapping("/teacherSaveStudent")
    public Integer teacherSaveStudent(String teacherId,String epid,String studentArr, String groupId){
        return iStudentService.teacherSaveStudent(teacherId,epid,studentArr, groupId);
    }

    /**
     * 老师端的学生批量报名
     * teacherId:老师id  操作人id
     * epid：赛事id
     * klassId：班级id
     * groupId: 赛事小组
     * */
    @RequestMapping("/applyStudentsByKlassId")
    public Integer applyStudentsByKlassId(String teacherId,String epid,String klassId, String groupId){
        return iStudentService.applyStudentsByKlassId(teacherId,epid,klassId, groupId);
    }

    /**
     *  老师端：查询老师报名的学生
     * eptitle：赛事标题
     * epsponsor 主办方
     * epstate 赛事状态
     * eplevel 赛事级别
     * teacherId:教师id
     */
    @GetMapping("selectEventsAndAggreTeacher")
    public PageInfo<Student> selectEventsAndAggreTeacher(Student student,Integer pageNum,Integer pageSize){
        System.out.println("selectEventsAndAggreTeacher == " + student);
        List<Student> studentList = iStudentService.selectEventsAndAggreTeacher(student,pageNum,pageSize);
        PageInfo pageInfo = new PageInfo(studentList);
        return pageInfo;
    }

    /**
     * 根据学生的id 查询该学生没有发布的作品
     * */
    @RequestMapping("selectWorksByUserId")
    public List<Works> selectWorksByUserId(String userId){
        return iStudentService.selectWorksByUserId(userId);
    }

    /**
     * 提交作品接口，向mysql中的work表记录一下
     * userId:用户的id
     * epId：报名的赛事id
     * wordId：作品id mongodb中的work_id
     * groupId： 赛事分组ID
     * */
    @RequestMapping("saveUnrelsByUserId")
    public Integer deleteUnrelsByUserId(String userId,Integer epId,String wordId, Integer groupId){
        try {
            return iStudentService.submitWorkForEvent(userId,epId,wordId,groupId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 根据userId  查询到用户所在学校的指导老师
     * */
    @RequestMapping("selectUserById")
    public List<Users> selectUserById(String userId){
        return iStudentService.selectUserById(userId);
    }

    /**
     * 学生端的赛事报名
     * String teacherId,String epId,String userId
     * */
    @RequestMapping("/saveStudent")
    public Integer saveStudent(Student student){
        log.info(""+student);
        return iStudentService.saveStudent(student);
    }

    /**
     * 校园赛事公告
     * 根据学生的id  查询学生所在学校的校园级
     * */
    @RequestMapping("selectManageByEpschoolAndEplevel")
    public PageInfo selectManageByEpschoolAndEplevel(String userId,Integer pageNum,Integer pageSize){
        List<SysAnnounManagement> sysAnnounManagementList = iStudentService.selectManageByEpschoolAndEplevel(userId,pageNum,pageSize);
        PageInfo pageInfo = new PageInfo<>(sysAnnounManagementList);
        return pageInfo;
    }

    /**
     * 校园赛事咨询
     * 根据学生的id  查询学生所在学校的校园级 的比赛
     * */
    @RequestMapping("selectEpByEpschoolAndEplevel")
    public PageInfo selectEpByEpschoolAndEplevel(String userId,Integer pageNum,Integer pageSize){
        List<SyseEventsPosted> syseEventsPostedList = iStudentService.selectEpByEpschoolAndEplevel(userId,pageNum,pageSize);
        PageInfo pageInfo = new PageInfo<>(syseEventsPostedList);
        return pageInfo;
    }

    /**
     * 根据用户的id查询当前人的信息
     * */
    @RequestMapping("selectByUserId")
    public Users selectByUserId(String userId){
        return iStudentService.selectByUsersId(userId);
    }

    /**
     * 删除报名赛事的作品
     * */
    @RequestMapping("deleteUserIdAndWord")
    public Integer deleteUserIdAndWord(String userId,Integer epId){
        return iStudentService.deleteUserIdAndWord(userId,epId);
    }

    /**
     * 修改学生信息
     * */
    @RequestMapping("updateUser")
    public AjaxResult updateUser(Users users,MultipartFile file){
        //1.判断验证和手机是否为空
        if(StringUtils.isNotEmpty(users.getPhoneNum()) && StringUtils.isNotEmpty(users.getCodeNum())){
            //2.判断验证和手机在redis中是否过期
            String code = redisTemplate.opsForValue().get(users.getPhoneNum());
            if(code != null){
                //2.如果不过期则修改
                return null;
            }else{
                AjaxResult.error("验证码失效,请重新发送");
            }
        }
       return AjaxResult.error("修改失败,请稍后再试!");
    }


    //mongodb中查询用户信息
    @RequestMapping("selectUsersById02")
    public Schools selectUsersById(String userId){
        Users users = studentDao.selectUsersByUserId(userId);
        return studentDao.selectSchoolsById(users.getSchool()==null?"":users.getSchool().toString());
    }

    /**
     * 晋级下一赛事
     * @return
     */
    @RequestMapping("upgradeNextStage")
    public AjaxResult upgradeNextStage(String epid, String studentIds, String eventStage) {
        try {
            iStudentService.upgradeNextStage(epid, studentIds, eventStage );
            return AjaxResult.success("晋级成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("晋级失败！");
        }
    }
}
